package com.ethan.crm.web.controller;

import com.ethan.crm.domain.Role;
import com.ethan.crm.page.PageResult;
import com.ethan.crm.query.BaseQueryObject;
import com.ethan.crm.query.RoleQueryObject;
import com.ethan.crm.service.IRoleService;
import com.ethan.crm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/role")
    public String index(){
        return "role";
    }


    @RequestMapping("/role_save")
    @ResponseBody
    public AjaxResult saveRole(Role role){
        AjaxResult ajaxResult ;
        try {
            roleService.insert(role);
            ajaxResult = new AjaxResult(true,"新增角色成功！");
        } catch (Exception e) {
            ajaxResult = new AjaxResult("新增角色失败，请联系管理员！");
        }
        return ajaxResult;
    }

    @RequestMapping("/role_update")
    @ResponseBody
    public AjaxResult updateRole(Role role){
        AjaxResult ajaxResult ;
        try {
            roleService.updateByPrimaryKey(role);
            ajaxResult = new AjaxResult(true,"更新角色成功！");
        } catch (Exception e) {
            ajaxResult = new AjaxResult("更新角色失败，请联系管理员！");
        }
        return ajaxResult;
    }

    @RequestMapping("/role_delete")
    @ResponseBody
    public AjaxResult deleteRole(Role role){
        AjaxResult ajaxResult ;
        try {
            roleService.deleteByPrimaryKey(role.getId());
            ajaxResult = new AjaxResult(true,"更新角色成功！");
        } catch (Exception e) {
            ajaxResult = new AjaxResult("更新角色失败，请联系管理员！");
        }
        return ajaxResult;
    }



    @RequestMapping("/role_list")
    @ResponseBody
    public PageResult queryRoleList(RoleQueryObject po){
        return roleService.queryForPage(po);
    }

    @RequestMapping("/role_queryForEmployee")
    @ResponseBody
    public List<Role> queryForEmp(){
        List<Role> roles = roleService.selectAll();
        return roles;
    }

    @RequestMapping("/role_queryByEid")
    @ResponseBody
    public List<Long> queryRoleByEid(Long eid){
        return roleService.queryRolesByEid(eid);
    }


}
